<?php
    /**商品表
     * Created by PhpStorm.
     * User: huangzhongwei
     * Date: 2018/10/24
     * Time: 9:07
     */

    namespace app\api\controller;

    use app\api\validate\Goods as GoodsValidate;
    use app\api\validate\IDMustBePositiveInt;
    use app\api\validate\PagingParameter;
    use app\common\model\GoodsType;
    use app\common\model\Goods as GoodsModel;
    use app\lib\exception\MissException;
    use app\lib\exception\SuccessMessage;

    class Goods extends BaseController
    {
        protected $beforeActionList = [
            'checkSuperScope' => ['only' => 'creategoods'],
        ];

        /**
         * 创建商品
         * @return SuccessMessage
         * @throws MissException
         * @throws \app\lib\exception\ParameterException
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        public function createGoods()
        {
            (new GoodsValidate())->goCheck();
            $data       = input('post.');
            $goods_type = (new GoodsType())->where('id', $data['goods_type_id'])->find();

            if (!$goods_type) {
                throw new MissException(['msg' => '商品类型不存在', 'errorCode' => 30000]);
            }
            $data['images'] = $data['images'] ?: request()->domain() . config('setting.img_default');


            (new GoodsModel())->save($data);

            return new SuccessMessage();

        }


        /**
         * 编辑商品
         * @return SuccessMessage
         * @throws MissException
         * @throws \app\lib\exception\ParameterException
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        public function editGoods()
        {
            (new IDMustBePositiveInt())->goCheck();
            (new GoodsValidate())->goCheck();
            $data       = input('post.');
            $goods_type = (new GoodsType())->where('id', $data['goods_type_id'])->find();
            if (!$goods_type) {
                throw new MissException(['msg' => '商品类型不存在', 'errorCode' => 30000]);
            }
            $data['images'] = $data['images'] ?: request()->domain() . config('setting.img_default');
            $goods          = (new GoodsModel())->get($data['id']);
            if (!$goods) {
                throw new MissException(['msg' => '商品不存在', 'errorCode' => 90000]);
            }
            $goods->save($data);

            return new SuccessMessage();
        }

        /**
         *
         * 删除商品
         * @return SuccessMessage
         * @throws \app\lib\exception\ParameterException
         */
        public function delete()
        {
            (new IDMustBePositiveInt())->goCheck();
            $data = input('post.id');
            GoodsModel::destroy($data);
            return new SuccessMessage();
        }


        /**
         *
         * 获取所有商品
         *
         * @param int $page
         * @param int $size
         *
         * @return array
         * @throws \app\lib\exception\ParameterException
         * @throws \think\exception\DbException
         */
        public function getAllGoods($page = 1, $size = 30)
        {
            (new PagingParameter())->goCheck();
            $users = (new  GoodsModel())->with('goodsType.university')->order('create_time', 'desc')->paginate(
                $size, false, [
                'page' => $page,
            ]);

            if ($users->isEmpty()) {
                // 对于分页最好不要抛出MissException，客户端并不好处理
                return [
                    'current_page' => $users->currentPage(),
                    'data'         => [],
                ];
            }
            $data = $users->toArray();
            return [
                'current_page' => $users->currentPage(),
                'data'         => $data,
            ];
        }


    }